using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {
	public Transform Player;	// Prefab
	public Transform Enemy;		// Prefab
	public Transform BigEnemy; 	// Prefab
	public Transform HealthPowerup; // Prefab
	
	public float PlayerRespawnTimer = 3.0f;
	public float EnemyRespawnTimer = 1.0f;
	public int PreferedEnemyCount = 1;
	public int MaxEnemyCount = 5;
	public int MaxPlayerLifeCount = 3;
	
	
	private int   PlayerLifeLeft;
	private int   CurrentEnemyCount;
	private float PlayerRespawnTime;
	private float EnemyRespawnTime;
	private int lastTime = -1;
	private int GameScore;
	private int ScoreToNextLevel = 300;
	private int EnemyKilled = 0;
	private bool bSpawnBossNext = false;
	
	// Use this for initialization
	void Start () {
		Screen.lockCursor = true;
		
		PlayerLifeLeft = MaxPlayerLifeCount;
		PlayerRespawnTime = 0.0f;
		EnemyRespawnTime = 0.0f;
		CurrentEnemyCount = 0;
		GameScore = 0;
		EnemyKilled = 0;
		UpdateScore( 0 );
		UpdateLifeCount( 0 );
	
		// change font color
		Color col;
		col.r = 0.333f;
		col.g = 1.0f;
		col.b = 0.333f;
		col.a = 0.75f;
		GameObject text = GameObject.Find( "CenterText" );
		text.renderer.material.color = col;
		text = GameObject.Find( "CenterText2" );
		text.renderer.material.color = col;
		text = GameObject.Find( "ScoreText" );
		text.renderer.material.color = col;
		text = GameObject.Find( "HealthText" );
		text.renderer.material.color = col;
		text = GameObject.Find( "LifeText" );
		text.renderer.material.color = col;

		col.r = 1.0f;
		text = GameObject.Find( "EnergyText" );
		text.renderer.material.color = col;
	}

	// Update is called once per frame
	void Update () {
		// Check Player
		CheckPlayerState();
		
		// Check enemy
		CheckEnemyState();
	
		// if game over
		if( PlayerLifeLeft < 0 )
		{
			if( Input.GetButtonDown( "Jump" ) )
			{
				Application.LoadLevel( 0 );
			}
		}
	}

	// Check Player State
	void CheckPlayerState()
	{
		if( !GameObject.FindWithTag( "Player" ) && PlayerLifeLeft >= 0 )
		{
			int time = (int )Mathf.Ceil( PlayerRespawnTime - Time.timeSinceLevelLoad );
			GameObject text = GameObject.Find( "CenterText" );
			TextMesh mesh = (TextMesh)text.GetComponent( "TextMesh" );
			
			if( time != lastTime )
			{
				mesh.text = "Respawn in " + time;
				lastTime = time;
			}
			
			if( Time.timeSinceLevelLoad > PlayerRespawnTime )
			{
				Instantiate( Player );
				mesh.text = "";
			}
		}
	}
	
	// Check Enemy State
	void CheckEnemyState()
	{
		if( CurrentEnemyCount < PreferedEnemyCount )
		{
			if( Time.timeSinceLevelLoad > EnemyRespawnTime )
			{
				if( bSpawnBossNext )
					Instantiate ( BigEnemy );
				else
					Instantiate( Enemy );
	
				CurrentEnemyCount ++;
				bSpawnBossNext = false;
			}
		}
	}

	// Receive Player death
	void NotifyPlayerDeath()
	{
		UpdateLifeCount( -1 );
		PlayerRespawnTime = Time.timeSinceLevelLoad + PlayerRespawnTimer;
		lastTime = -1;
	}
	
	// Receive Enemy Death ()
	void NotifyEnemyDeath( GameObject obj )
	{
		CurrentEnemyCount--;
		EnemyKilled ++;
		EnemyRespawnTime = Time.timeSinceLevelLoad + EnemyRespawnTimer;
		
		GameObject player = GameObject.FindWithTag( "Player" );
		if( player )
			player.SendMessage( "ChangeEnergy", 200.0f );
	
		if( EnemyKilled % 6 == 0 )
			bSpawnBossNext = true;
	
		if( obj.name.Contains( "BigEnemy" ) )
			Instantiate( HealthPowerup, obj.transform.position, obj.transform.rotation );
	}
	
	// Update game score
	void UpdateScore( int score )
	{
		GameScore += score;
		GameObject text = GameObject.Find( "ScoreText" );
		TextMesh mesh = (TextMesh)text.GetComponent( "TextMesh" );
		mesh.text = "Score: " + GameScore;
		
		// Increase enemy count by score
		if( GameScore > ScoreToNextLevel )
		{
			PreferedEnemyCount++;
			ScoreToNextLevel += PreferedEnemyCount * 500;
			if( PreferedEnemyCount > MaxEnemyCount )
				PreferedEnemyCount = MaxEnemyCount;
		}
	}

	// Update Life left count
	void UpdateLifeCount( int life )
	{
		PlayerLifeLeft += life;
		
		GameObject text = GameObject.Find( "LifeText" );
		TextMesh mesh = (TextMesh)text.GetComponent( "TextMesh" );
		mesh.text = "LIFE x " + (int)Mathf.Max( 0, PlayerLifeLeft );

		if( PlayerLifeLeft < 0 )
			GameOver();
	}

	// GameOver!!
	void GameOver()
	{
		GameObject text = GameObject.Find( "CenterText" );
		TextMesh mesh = (TextMesh)text.GetComponent( "TextMesh" );
		mesh.text = "GAME OVER";
		text = GameObject.Find( "CenterText2" );
		mesh = (TextMesh)text.GetComponent( "TextMesh" );
		mesh.text = "Press SPACE to exit...";
	}
}

